#include "stdafx.h"
#include <XTextLine.h>


void XTextLine::_loadVertices()
{
	if(Render != NULL)
	{
		XVertex* vert = new XVertex[4];

		vert[0].X = Rectangle.X;
		vert[0].Y = Rectangle.Y;
		vert[1].X = Rectangle.X + Rectangle.W;
		vert[1].Y = Rectangle.Y;
		vert[2].X = Rectangle.X; 
		vert[2].Y = Rectangle.Y + Rectangle.H;
		vert[3].X = Rectangle.X + Rectangle.W;
		vert[3].Y = Rectangle.Y + Rectangle.H;

		Render->SetVertex(vert, 4);
		delete[] vert;
	}
}

void XTextLine::_loadTextureVertices()
{

	if(Render != NULL)
	{
		XVertex* vert = new XVertex[4];

		if(Active == false)
		{
			vert[0].X = (float)512/ParentWindow->GetTexture()->GetWidth();
			vert[0].Y = (float)258/ParentWindow->GetTexture()->GetHeight();
			vert[1].X = (float)715/ParentWindow->GetTexture()->GetWidth();
			vert[1].Y = (float)258/ParentWindow->GetTexture()->GetHeight();
			vert[2].X = (float)512/ParentWindow->GetTexture()->GetWidth();
			vert[2].Y = (float)290/ParentWindow->GetTexture()->GetHeight();
			vert[3].X = (float)715/ParentWindow->GetTexture()->GetWidth();
			vert[3].Y = (float)290/ParentWindow->GetTexture()->GetHeight();
		}
		else
		{
			vert[0].X = (float)512/ParentWindow->GetTexture()->GetWidth();
			vert[0].Y = (float)294/ParentWindow->GetTexture()->GetHeight();
			vert[1].X = (float)715/ParentWindow->GetTexture()->GetWidth();
			vert[1].Y = (float)294/ParentWindow->GetTexture()->GetHeight();
			vert[2].X = (float)512/ParentWindow->GetTexture()->GetWidth();
			vert[2].Y = (float)327/ParentWindow->GetTexture()->GetHeight();
			vert[3].X = (float)715/ParentWindow->GetTexture()->GetWidth();
			vert[3].Y = (float)327/ParentWindow->GetTexture()->GetHeight();
		}

		Render->SetTexture(ParentWindow->GetTexture(), vert, 4);
		delete[] vert;
	}
}

XTextLine::XTextLine(XHash hash, XWindow* parentWindow)
	: XWidget(hash, parentWindow)
{
	Active = false;
	LastKey = -1;
	Pause = 0;

	Render = new XRenderPack(hash.String + "-Layout");
	Text = new XTextPack(hash.String + "-Text");
	Text->SetFont(ParentWindow->GetFont());

	ParentWindow->RegisterControlsRender(Render);
	ParentWindow->RegisterControlsText(Text);

	SetGeometry(XRectangle(100, 100, 100, 30));
	_loadTextureVertices();

}

void XTextLine::SetText(const char* text)
{
	Text->SetText(text);
}

void XTextLine::SetText(int number)
{
	Text->SetText(number);
}

void XTextLine::SetActive()
{
	Active = true;
	_loadTextureVertices();
}

void XTextLine::SetUnactive()
{
	Active = false;
	_loadTextureVertices();
}

void XTextLine::SetGeometry(XRectangle rect)
{
	Rectangle = rect;
	Rectangle.H = 30;

	Text->SetTextBox(XRectangle(Rectangle.X + 5, Rectangle.Y + 5, Rectangle.W - 5, Rectangle.H - 5));

	_loadVertices();
}

void XTextLine::Update(XInputModule* input)
{
	if(Active && Enabled)
	{
		for(int i=7; i<128; i++)
		{
			if(input->GetButton(i).GetCurrent() == true)
			{
				if(input->GetButton(i).GetPrevious() != true)
				{
					if(i == XKEY_BACKSPACE && TextData.length() > 0)
						TextData.erase(TextData.length() - 1, 1 );
					else if(i != XKEY_BACKSPACE && i != XKEY_ENTER)
						TextData += i;

					Pause = 0;
					LastKey = -1;
					Text->SetText(TextData);
				}
				else
				{
					if(Pause < 20)
					{
						LastKey = i;
					}
					else if(LastKey == i && Pause>20)
					{
						if(i == XKEY_BACKSPACE && TextData.length() > 0)
							TextData.erase(TextData.length() - 1, 1 );
						else if(i != XKEY_BACKSPACE && i != XKEY_ENTER)
							TextData += i;
					}
					
					Text->SetText(TextData);
				}

				
			}
				
		}
		Pause++;
	}

	Render->SetTranslation(XVector(ParentWindow->GetGeometry()->X, ParentWindow->GetGeometry()->Y));
	Text->SetTranslation(XVector(ParentWindow->GetGeometry()->X, ParentWindow->GetGeometry()->Y));
}